<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-19
 ***********************************************************************/
if(defined("IVANENGINE_CORE_CALLBACK"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_CALLBACK", true);

require_once (dirname(__FILE__).'/Common.php');

class CCallback extends CObject 
{
	protected $m_method   = null;
    protected $m_object   = null;
    protected $m_param    = array();
	public function __construct($method, $object = null) 
	{
		parent::__construct();
		$this->m_method   = $method;
        $this->m_object   = $object;
	}
	
	public function __destruct() 
	{
		unset($this->m_method);
        unset($this->m_object);
        unset($this->m_param);
		parent::__destruct();
	}
	
	public function GetMethod()
	{
		return $this->m_method;
	}
    
    public function GetObject()
    {
        return $this->m_object;
    }
    
    public function GetParam()
    {
        return $this->m_param;
    }
    
    public function SetParam($param)
    {
        $this->m_param = $param;
    }
	
	public function Call()
	{
        if( func_num_args() > 0 ){
    	    $arguments = func_get_args();
        }else{
            $arguments = $this->m_param;
        } 
        if( count($arguments) == 1){
            $arguments  = $arguments[0];
        }
        if( $this->m_object ){
            return call_user_func(array($this->m_object, $this->m_method), $arguments);
        }else{
    	    return call_user_func($this->m_method, $arguments);
        }
	}
    
    public function __clone()
    {
        $instance = new CCallback($this->m_method, $this->m_object);
        $instance->SetParam($this->GetParam());
        return $instance;
    }
}

?>